package com.shunhe.storage.ui.repo;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.support.view.LinearItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.Customer;
import com.shunhe.storage.dao.model.Goods;
import com.shunhe.storage.ui.adapter.SwipeCustomerListAdapter;
import com.shunhe.storage.ui.adapter.SwipeGoodsListAdapter;
import com.shunhe.storage.ui.contact.ChCustomerActivity;
import com.shunhe.storage.ui.goods.ChGoodsActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class OutRepoOrderActivity extends BaseActivity {
    private RecyclerView rcv_goods_list, rcv_cust;// 选择的货品列表 选择的客户
    private TextView tv_ch_goods_0, tv_ch_cust_0;// 选择/添加货品,选择客户

    private SwipeGoodsListAdapter adapter;
    private List<Goods> goodsList = new ArrayList<>();

    private SwipeCustomerListAdapter custAdapter;
    private List<Customer> customerList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_out_repo_order;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_goods_list = findViewById(R.id.rcv_goods_list);
        rcv_cust = findViewById(R.id.rcv_cust);
        tv_ch_goods_0 = findViewById(R.id.tv_ch_goods_0);
        tv_ch_cust_0 = findViewById(R.id.tv_ch_cust_0);

        setHeadTitle("出库下单");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new SwipeGoodsListAdapter(this);
        LinearItemDecoration divider = new LinearItemDecoration.Builder(this)
                .setSpan(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
                .build();
        rcv_goods_list.addItemDecoration(divider);
        rcv_goods_list.setLayoutManager(manager);
        rcv_goods_list.setAdapter(adapter);
        for (int i = 0; i < 3; i++) {
            goodsList.add(new Goods());
        }
        adapter.setDataList(goodsList);

        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        custAdapter = new SwipeCustomerListAdapter(this);
        LinearItemDecoration divider2 = new LinearItemDecoration.Builder(this)
                .setSpan(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
                .setShowLastLine(true)
                .build();
        rcv_cust.addItemDecoration(divider2);
        rcv_cust.setLayoutManager(manager2);
        rcv_cust.setAdapter(custAdapter);
        custAdapter.setDataList(customerList);

        setListeners();
        Arad.bus.register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(EventModel ev) {
        if (ev.getEventCode() == 20120552) {// 选择客户完成
            customerList.clear();
            customerList.add(new Customer());
            custAdapter.setDataList(customerList);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    private void setListeners() {
        setOnClickListener(tv_ch_goods_0, tv_ch_cust_0);
        adapter.setOnSwipeListener(new SwipeGoodsListAdapter.OnSwipeListener() {
            @Override
            public void onDel(Object obj, int index) {
                Goods goods = (Goods) obj;
                ToastUtils.showShort("删除  index = " + index);
                goodsList.remove(index);
                adapter.setDataList(goodsList);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ch_goods_0) {// 选择货品
            startActivity(ChGoodsActivity.class);
        }
        if (v == tv_ch_cust_0) {// 选择客户
            startActivity(ChCustomerActivity.class);
        }
    }
}